package com.muyu.system.controller;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.DataPageResp;
import com.muyu.common.core.web.page.PageQueryModel;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.InnerAuth;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.system.domain.model.SysOperLogAddModel;
import com.muyu.system.domain.model.SysOperlogPageQueryModel;
import com.muyu.system.domain.req.SysOperLogAddReq;
import com.muyu.system.domain.req.SysOperlogListReq;
import com.muyu.system.domain.resp.SysOperlogListResp;
import com.muyu.system.service.SysOperLogService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 操作日志记录
 *
 * @author muyu
 */
@RestController
@RequestMapping("/operlog")
public class SysOperlogController extends BaseController {

    /**
     * 操作日志记录 服务对象
     */
    @Autowired
    private SysOperLogService operLogService;

    /**
     * 操作日志记录列表
     *
     * @param sysOperlogListReq 操作日志记录列表查询请求对象
     * @return 列表数据
     */
    @RequiresPermissions("system:operlog:list")
    @PostMapping("/list")
    public Result<DataPageResp<SysOperlogListResp>> list (@RequestBody SysOperlogListReq sysOperlogListReq) {
        PageQueryModel<SysOperlogListResp> sysOperLogPageQueryModel = operLogService.pageQuery(
                SysOperlogPageQueryModel.reqBuild(sysOperlogListReq)
        );
        return Result.success(DataPageResp.of(sysOperLogPageQueryModel));
    }

    /**
     * 操作日志记录导出
     *
     * @param response 响应对象
     * @param sysOperlogListReq 操作日志记录列表查询请求对象
     */
    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:operlog:export")
    @PostMapping("/export")
    public void export (HttpServletResponse response, SysOperlogListReq sysOperlogListReq) {
        // TODO 导出需要重新编写
        PageQueryModel<SysOperlogListResp> sysOperLogPageQueryModel = operLogService.pageQuery(
                SysOperlogPageQueryModel.reqBuild(sysOperlogListReq)
        );
        ExcelUtil<SysOperlogListResp> util = new ExcelUtil<>(SysOperlogListResp.class);
        util.exportExcel(response, sysOperLogPageQueryModel.getDataList(), "操作日志");
    }

    /**
     * 批量删除操作日志记录
     *
     * @param operIds 操作日志记录的id集合
     */
    @Log(title = "操作日志", businessType = BusinessType.DELETE)
    @RequiresPermissions("system:operlog:remove")
    @DeleteMapping("/{operIds}")
    public Result<String> remove (@PathVariable("operIds") List<Long> operIds) {
        operLogService.deleteOperLogByIds(operIds);
        return Result.success();
    }

    /**
     * 清空操作日志记录
     */
    @RequiresPermissions("system:operlog:clean")
    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public Result<String> clean () {
        operLogService.cleanOperLog();
        return success();
    }

    /**
     * 新增操作日志记录
     *
     * @param operLogAddReq 操作日志记录新增请求对象
     */
    @InnerAuth
    @PostMapping
    public Result<String> add (@RequestBody SysOperLogAddReq operLogAddReq) {
        operLogService.insertOperlog(SysOperLogAddModel.addReqBuildToModel(operLogAddReq));
        return Result.success();
    }
}
